//
//  LRXOffline.m
//  路人侠
//
//  Created by zhangxulong on 4/19/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXOffline.h"

#define offlineImageW  306 * wScale
#define offlineImageH  266 * hScale

#define btnTouchViewW   200 * wScale
#define btnTouchViewH   50 * hScale

#define viewInterH  50 * hScale
@implementation LRXOfflineView

- (instancetype)initNoDataWithFrame:(CGRect)frame description:(NSString *)description canTouch:(BOOL)isCanTouch
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = frame;
        
        UIImageView * offLineImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"offline"]];
        offLineImage.frame = CGRectMake((frame.size.width - offlineImageW) * 0.5, (frame.size.height - offlineImageH) * 0.4, offlineImageW, offlineImageH);
        
        UILabel * offlineLabel = [[UILabel alloc]init];
        offlineLabel.text = description;
        offlineLabel.textColor = [UIColor colorWithRed:182 green:182 blue:182];
        offlineLabel.font = [UIFont systemFontOfSize:28 * hScale];
        [offlineLabel sizeToFit];
        offlineLabel.center = CGPointMake(offLineImage.center.x, CGRectGetMaxY(offLineImage.frame) + viewInterH);
        
        if (isCanTouch) {
            
            UIButton *btnTouchView = [[UIButton alloc] init];
            btnTouchView.layer.cornerRadius = 5 * hScale;
            btnTouchView.layer.borderColor = [UIColor colorWithRed:182 green:182 blue:182].CGColor;
            btnTouchView.layer.borderWidth = 2 * hScale;
            btnTouchView.frame = CGRectMake(0, 0, btnTouchViewW, btnTouchViewH);
            
            [btnTouchView setTitle:@"重新加载" forState:UIControlStateNormal];
            [btnTouchView setTitleColor:[UIColor colorWithRed:182 green:182 blue:182] forState:UIControlStateNormal];
            btnTouchView.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
            
            btnTouchView.center = CGPointMake(offLineImage.center.x, CGRectGetMaxY(offlineLabel.frame) + viewInterH);
            
            [btnTouchView addTarget:self action:@selector(windowTouchNoDataView) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btnTouchView];
        }
        
        [self addSubview:offlineLabel];
        [self addSubview:offLineImage];
    }
    return  self;
}

- (instancetype)initNoInternetWithFrame:(CGRect)frame description:(NSString *)description canTouch:(BOOL)isCanTouch
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = frame;
        
        UIImageView * offLineImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"offline"]];
        offLineImage.frame = CGRectMake((frame.size.width - offlineImageW) * 0.5, (frame.size.height - offlineImageH) * 0.4, offlineImageW, offlineImageH);
        
        UILabel * offlineLabel = [[UILabel alloc]init];
        offlineLabel.text = description;
        offlineLabel.textColor = [UIColor colorWithRed:182 green:182 blue:182];
        offlineLabel.font = [UIFont systemFontOfSize:28 * hScale];
        [offlineLabel sizeToFit];
        offlineLabel.center = CGPointMake(offLineImage.center.x, CGRectGetMaxY(offLineImage.frame) + viewInterH);
        
        if (isCanTouch) {
            
            UIButton *btnTouchView = [[UIButton alloc] init];
            btnTouchView.layer.cornerRadius = 5 * hScale;
            btnTouchView.layer.borderColor = [UIColor colorWithRed:182 green:182 blue:182].CGColor;
            btnTouchView.layer.borderWidth = 2 * hScale;
            btnTouchView.frame = CGRectMake(0, 0, btnTouchViewW, btnTouchViewH);
            
            [btnTouchView setTitle:@"重新加载" forState:UIControlStateNormal];
            [btnTouchView setTitleColor:[UIColor colorWithRed:182 green:182 blue:182] forState:UIControlStateNormal];
            btnTouchView.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
            
            btnTouchView.center = CGPointMake(offLineImage.center.x, CGRectGetMaxY(offlineLabel.frame) + viewInterH);
            
            [btnTouchView addTarget:self action:@selector(windowTouchOfflineAction) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btnTouchView];
        }
        
        [self addSubview:offlineLabel];
        [self addSubview:offLineImage];
        
    }
    return  self;
}


- (instancetype)initImageWithFrame:(CGRect)frame image:(UIImage *)image description:(NSString *)description canTouch:(BOOL)isCanTouch
{
    if (self = [super init]) {
        
    }
    return  self;
}

- (instancetype)initImageNameWithFrame:(CGRect)frame imageName:(NSString *)imageName description:(NSString *)description canTouch:(BOOL)isCanTouch
{
    if (self = [super init]) {
        
    }
    return  self;
}

- (void)windowTouchOfflineAction
{
    if ([self.offlineDelegate respondsToSelector:@selector(didTouchOfflineView)]) {
        
        [self.offlineDelegate didTouchOfflineView];
    }
}
- (void)windowTouchNoDataView
{
    if ([self.offlineDelegate respondsToSelector:@selector(didTouchNoDataView)]) {
        [self.offlineDelegate didTouchNoDataView];
    }
}

+ (void)showOfflineView:(NSString *)text
{
    [LRXOfflineView showOfflineView:text toView:nil];
}


+ (void)showOfflineView:(NSString * )text toView:(UIView *)view
{
    if (view ==  nil)
        view = [[UIApplication sharedApplication].windows lastObject];
    UIView * offlineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300 * wScale, 150 * hScale)];
    
    offlineView.layer.cornerRadius = 10 * hScale;
    offlineView.layer.masksToBounds = YES;
    
    UIView * backView = [[UIView alloc]initWithFrame:offlineView.frame];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.74;
    
    
    
    CGRect labelFrame = CGRectMake(offlineView.frame.origin.x + 10 * wScale, offlineView.frame.origin.y + 10 * wScale, offlineView.frame.size.width - 20  * wScale, offlineView.frame.size.height - 20 * hScale);
    UILabel * offlineLabel = [[UILabel alloc]initWithFrame:labelFrame];
    offlineLabel.text = text;
    offlineLabel.font = [UIFont systemFontOfSize:25 * hScale];
    offlineLabel.textColor = [UIColor whiteColor];
    offlineLabel.textAlignment = NSTextAlignmentCenter;
    offlineLabel.numberOfLines = 0;
    offlineLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    
    CAKeyframeAnimation * animation= [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@0,@1];
    animation.duration = 0.1;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
    [offlineView.layer addAnimation:animation forKey:nil];
    
    [offlineView addSubview:backView];
    [offlineView addSubview:offlineLabel];
    offlineView.center = view.center;
    [view addSubview:offlineView];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        animation.values = @[@1,@0];
        [offlineView.layer addAnimation:animation forKey:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [offlineView removeFromSuperview];
        });
        
    });
}
+ (void)showAlerView:(NSString *)text toView:(UIView *)view
{
    if (view ==  nil) view = [[UIApplication sharedApplication].windows lastObject];
        
    UIView * offlineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200 * wScale, 250 * hScale)];
    offlineView.layer.cornerRadius = 10 * hScale;
    offlineView.layer.masksToBounds = YES;
  
    offlineView.center = CGPointMake(MainScreenW * 0.5, MainScreenH  * 0.5 );

    UIView * backView = [[UIView alloc]initWithFrame:offlineView.bounds];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.75;
    
    UIImageView * duigou = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"duigou"]];
    duigou.frame = CGRectMake(20 * wScale, 0, offlineView.frame.size.width - 40  * wScale, offlineView.frame.size.height - 50 * hScale);
    
    
    CGRect labelFrame = CGRectMake(10 * wScale, CGRectGetMaxY(duigou.frame), offlineView.frame.size.width - 20  * wScale, offlineView.frame.size.height - 20 * hScale - CGRectGetMaxY(duigou.frame));
    UILabel * offlineLabel = [[UILabel alloc]initWithFrame:labelFrame];
    offlineLabel.text = text;
    offlineLabel.font = [UIFont systemFontOfSize:25 * hScale];
    offlineLabel.textColor = [UIColor whiteColor];
    offlineLabel.textAlignment = NSTextAlignmentCenter;
    
    CAKeyframeAnimation * animation= [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@0,@1];
    animation.duration = 0.1;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
    [offlineView.layer addAnimation:animation forKey:nil];
    
    [offlineView addSubview:backView];
    [offlineView addSubview:offlineLabel];
    [offlineView addSubview:duigou];
    [view addSubview:offlineView];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        animation.values = @[@1,@0];
        [offlineView.layer addAnimation:animation forKey:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [offlineView removeFromSuperview];
        });
        
    });
}
@end
